<?php

    error_reporting(E_ALL^E_STRICT);
    ini_set( "display_errors", 1 ) ;
    
    define( "ARROW_ABSOLUTE_PATH", dirname(__FILE__)."/server"); 
    define( "ARROW_CORE_PATH", ARROW_ABSOLUTE_PATH."/core" );
    define( "ARROW_CACHE_PATH", "./cache" );
    define( "ARROW_MODELS_PATH", ARROW_ABSOLUTE_PATH."/standardModels" );
    define( "ARROW_CONF_PATH", ARROW_ABSOLUTE_PATH."/conf" );
    define( "ARROW_PROJECTS_PATH", "./projects" );
    define( "ARROW_LOG_PATH", "./log" );

    define( "SUPER_CACHE", false );

    date_default_timezone_set("Europe/Warsaw") ;

    require_once(ARROW_CORE_PATH."/ArrowObject.php");
    require_once(ARROW_CORE_PATH."/ArrowLogger.php");
    require_once(ARROW_CORE_PATH."/ArrowException.php");
    require_once(ARROW_CORE_PATH."/ArrowCoreException.php");
    require_once(ARROW_CORE_PATH."/ArrowConfProvider.php");
    require_once(ARROW_CORE_PATH."/ArrowCacheProvider.php");
    require_once(ARROW_CORE_PATH."/ArrowController.php");

    $includedModels = ArrowCacheProvider::importModelsCache();
    ArrowController::setLoadedModels($includedModels);

    require_once(ARROW_CORE_PATH."/ArrowRequestContext.php");
    require_once(ARROW_CORE_PATH."/ArrowStateManager.php");
    require_once(ARROW_CORE_PATH."/ArrowRouter.php");
    require_once(ARROW_CORE_PATH."/ArrowTemplateParser.php");
    require_once(ARROW_CORE_PATH."/ArrowView.php");

    

    ArrowController::importModel("utils.temporary.Temporary");
    ArrowController::importModel("utils.track.Track");
    
    
//------------------------------------- funkcja ------------------------------------------------------

      function propagateClean( $tmp_id, $model, $file ) {
        fwrite( $file, " $tmp_id => $model \n" );
        ArrowController::importModel( $model ) ;
        $class = ArrowModel::getModelClass( $model ) ;
        $crit = new Criteria();
        $crit->addCondition( "tmp_id", $tmp_id );			// pobierz wszystkie obiekty o zadanym tmp_id
        $objs = call_user_func(array( $class,'getByCriteria'), $crit, $class);
        if( count($objs) > 0 ) {
          if( $objs[0] instanceof ITemporary ) {
            $rmodels = $objs[0]->getRelatedModels() ;	// pobierz modele z nim powiązane
            foreach( $rmodels as $rmodel ){
              foreach( $objs as $obj ) {						// dla kazdego z obiektów usuń tracka oraz obiekt
                Track::cleanDelete( $obj["id"], $class ) ;
                $obj->delete() ;
                ArrowOrmSqlRouter::query("COMMIT;");
                ArrowOrmSqlRouter::query("BEGIN;");
              }
              propagateClean( $tmp_id, $rmodel, $file ) ;		// i wywołaj dla nich również oczyszczenie
            }
          }
          
          
          
        }
       
      }
//-----------------------------------------------------------------------------------------------------    
    
    
    
    
    ArrowRouter::$INDEX_FILE = basename( __FILE__ ) ;
    $router = ArrowRouter::getDefault( ArrowRequestContext::getDefault());
    $controler = ArrowController::getDefault( $router );
    
    $date = date("Y-m-d H-i-s") ;
    $shdate = date("Y-m-d");
    $file = fopen("./log/garbage-collector-log- $shdate.txt", "a" );
    
    fwrite( $file, "\n#@#!!-------$date ------\n" );
    
    $auth = Auth::getDefault();
    $auth->doLogin( "garbage_collector", "collector" );
    if( $auth->isLogged() ) {
    
    
    
 //  -----------------------------------------------------------------------------------------------------------------------------------------   
    
      $am = AccessManager::getDefault()->turnOff();
      Track::turnOff();
      $to_clean = Temporary::getByCriteria( new Criteria(), Temporary::TCLASS );
      
      $i = 1 ;
      echo count($to_clean)." <br />" ;
      foreach( $to_clean as $temporary ) {
         fwrite( $file, print_r( $temporary->serialize(), 1) );
        if( $i > 0 ) {
          propagateClean( $temporary[ Temporary::F_ID ], $temporary[Temporary::F_MODEL], $file ) ;
          
          $temporary->delete() ;
        }
        --$i;
      }
      
      //echo "<pre>"; 
       // echo count( $to_clean ) ."<br />";
      //  print_R( $to_clean );

    } else fwrite( $file, "nie zalogowano" );
   //  -----------------------------------------------------------------------------------------------------------------------------------------   

  
    
    
    
    
    
    
    echo "zakończono" ;

    
    
    
    $message = @ob_get_contents()."\n" ;
    fwrite( $file, $message );
    
    fclose($file);
    
    ArrowController::end();
?>